package site.wtcc.originalfavor.api.controller.osUser;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.*;
import site.wtcc.originalfavor.func.constant.enums.ResultCode;
import site.wtcc.originalfavor.func.common.persistence.JsonBean;
import site.wtcc.originalfavor.dao.entity.osUser.request.OsUserPageRequest;
import site.wtcc.originalfavor.biz.service.osUser.IOsUserService;
import site.wtcc.originalfavor.dao.entity.osUser.OsUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hxd
 * @since 2020-11-05
 */
@Api(tags = {""})
@RestController
@RequestMapping("//os-user")
public class OsUserController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private IOsUserService osUserService;


    @ApiOperation(value = "新增，对外用户注册")
    @PostMapping(value = "/add")
    public JsonBean add(@RequestBody OsUser request){
        try {
            osUserService.add(request);
            return JsonBean.returnResponse();
        } catch (Exception e) {
            log.error(e.getMessage());
            return JsonBean.returnResponse(false, ResultCode.SERVICE_EXCEPTION ,"注册失败");
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete/{id}")
    public int delete(@PathVariable("id") Long id){
        return osUserService.delete(id);
    }

    @ApiOperation(value = "更新")
    @PostMapping(value = "/update")
    public int update(@RequestBody OsUser request){
        return osUserService.updateData(request);
    }

    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码"),
        @ApiImplicitParam(name = "pageCount", value = "每页条数")
    })
    @GetMapping(value = "/findListByPage")
    public IPage<OsUser> findListByPage(@RequestParam Integer page,
                                   @RequestParam Integer pageCount){
        return osUserService.findListByPage(page, pageCount);
    }

    @ApiOperation(value = "id查询")
    @PostMapping(value = "/getPageById")
    public OsUser getPageById(@RequestBody OsUserPageRequest request){
        return osUserService.getPageById(request.getId());
    }

    @ApiOperation(value = "查询分页数据post,request需继承BasePageRequest")
    @PostMapping(value = "/getPage")
    public IPage<OsUser> getPage(@RequestBody OsUserPageRequest request){
        return osUserService.getPage(request);
    }

    @ApiOperation(value = "登录api")
    @PostMapping(value = "/login")
    public IPage<OsUser> login(@RequestBody OsUserPageRequest request){
        return osUserService.login(request);
    }

}
